package com.cloud.prac.service.inject;

import com.cloud.prac.model.InjectResponse;

import java.util.List;
import java.util.Map;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * spring 依赖注入方式
 *
 * @author 24373
 * @version 1.0
 * @date 2024-07-14 18:43
 */
@Service
@Slf4j
//@RequiredArgsConstructor
public class TestInjectService {

    private final List<InjectService> injectService;
    private final Map<String, InjectService> injectServiceMap;

    /**
     * 构造器注入可以将组件实现设置为不可变对象，并确保所依赖项不会丢失 null
     * https://docs.spring.io/spring-framework/reference/core/beans/dependencies/factory-collaborators.html
     */
    public TestInjectService(List<InjectService> injectService, Map<String, InjectService> injectServiceMap) {
        this.injectService = injectService;
        this.injectServiceMap = injectServiceMap;
    }

    public InjectResponse inject() {
        injectService.forEach(t -> log.info("{}", t));
        injectServiceMap.forEach((k, v) -> log.info("{}={}", k, v));
        return InjectResponse.builder()
                .injectService(injectService)
                .injectServiceMap(injectServiceMap)
                .build();
    }


}
